package com.swift.authority.application.controller;

import com.swift.authority.common.constant.CommonEnum;
import com.swift.authority.common.util.ExcelUtil;
import com.swift.authority.common.util.RedisUtil;
import com.swift.authority.core.user.request.UserReq;
import com.swift.authority.core.user.response.UserRes;
import com.swift.authority.service.user.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.swift.authority.common.response.ResponseResult;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * @author Administrator
 * @description :
 * @date 2022/06/19 13:45
 * @since open jdk 1.8
 */
@RestController
@RequestMapping("/users")
public class UserController {
    private Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private UserService userService;

    /**
     *  用户注册
     * @return ResponseResult<String>
     */
    @PostMapping("/registry")
    public ResponseResult<String> registry(@RequestBody UserReq user) {
        ResponseResult<String> response = new ResponseResult<>(CommonEnum.SUCCESS);
        try {
            userService.userAdd(user);
        } catch (Exception e) {
            logger.error("用户注册失败 ...", e);
            response.setMessage(CommonEnum.FAILURE.getDescription());
            response.setStatus(CommonEnum.FAILURE.getCode());
        }
        return response;
    }

    /**
     * 用户登录
     * @param user 用户信息
     * @return ResponseResult<UserRes>
     */
    @PostMapping("/login")
    public ResponseResult<UserRes> login(@RequestBody UserReq user) {
        ResponseResult<UserRes> response = new ResponseResult<>(CommonEnum.SUCCESS);
        try {
            UserRes userRes = userService.queryUserByUserName(user.getUsername());
            response.setData(userRes);
        } catch (Exception e) {
            logger.error("用户注册失败 ...", e);
            response.setMessage(CommonEnum.FAILURE.getDescription());
            response.setStatus(CommonEnum.FAILURE.getCode());
        }
        return response;
    }

    /**
     * 获取用户信息
     *
     * @param user 用户信息 分页
     * @return ResponseResult<UserRes>
     */
    @PostMapping("/getUserInfo")
    public ResponseResult<UserRes> getUserInfo(@RequestBody UserReq user) {
        ResponseResult<UserRes> response = new ResponseResult<>(CommonEnum.SUCCESS);
        try {
            UserRes userRes = userService.queryUserByUserName(user.getUsername());
            response.setData(userRes);
        } catch (Exception e) {
            logger.error("用户注册失败 ...", e);
            response.setMessage(CommonEnum.FAILURE.getDescription());
            response.setStatus(CommonEnum.FAILURE.getCode());
        }
        return response;
    }

    @GetMapping(value = "/downLoadExcel")
    public void downLoadExcel(HttpServletResponse response) {
        try {
            ExcelUtil.createExcel(response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @PostMapping(value = "/parseExcel")
    public void parseExcel(@RequestBody MultipartFile file) {
        try {

            Map<String, Object> stringListMap = ExcelUtil.parseExcel(file);
            System.out.println(stringListMap);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @PostMapping("/getUserResource")
    public Object getUserResource(Authentication authentication){
        return authentication.getPrincipal();
    }
}

